package com.guoning.office_system.service.impl;

import com.guoning.office_system.mapper.MessageMapper;
import com.guoning.office_system.mapper.UserMapper;
import com.guoning.office_system.model.Message;
import com.guoning.office_system.model.User;
import com.guoning.office_system.service.MessageService;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    MessageMapper messageMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public PageBean<Map> selectByQueryCondition(Criteria criteria, PageBean pageBean) {
        criteria.addExtField("pageIndex", pageBean.getPageSize() * pageBean.getPageIndex());
        criteria.addExtField("pageSize", pageBean.getPageSize());
        Long count = messageMapper.selectCountByQueryCondition(criteria);
        List<Message> messageList = messageMapper.selectByQueryCondition(criteria);
        List<Map> mapList = new ArrayList<>();
        for (int i=0; i<messageList.size(); i++){
            Map map = new HashMap();
            User sendUser = userMapper.selectByPrimaryKey(messageList.get(i).getSendUserId());
            User acceptUser = userMapper.selectByPrimaryKey(messageList.get(i).getAcceptUserId());
            map.put("id", messageList.get(i).getId());
            map.put("sendUserId", messageList.get(i).getSendUserId());
            map.put("acceptUserId", messageList.get(i).getAcceptUserId());
            map.put("messageCode", messageList.get(i).getMessageCode());
            map.put("sendTime", messageList.get(i).getSendTime());
            map.put("enableStatus", messageList.get(i).getEnableStatus());
            map.put("messageStatus", messageList.get(i).getMessageStatus());
            map.put("creator", messageList.get(i).getCreator());
            map.put("createTime", messageList.get(i).getCreateTime());
            map.put("dr", messageList.get(i).getDr());
            map.put("messageContent", messageList.get(i).getMessageContent());
            map.put("sendUser", sendUser);
            map.put("acceptUser", acceptUser);
            mapList.add(map);
        }
        pageBean.setTotalCount(count);
        pageBean.setResultList(mapList);
        return pageBean;
    }

    @Override
    public int updateAllMessageByUserId(Long userId) {
        return messageMapper.updateAllMessageByUserId(userId);
    }

    @Override
    public int updateHaveReadById(List<Message> list) {
        for (int i=0; i<list.size(); i++){
            if(list.get(i).getMessageStatus() != 1){
                list.remove(i);
            }else{
                list.get(i).setMessageStatus(2);
            }
        }
        int i = messageMapper.updateMessageStatusById(list);
        return i;
    }

    @Override
    public int updateDeleteById(List<Message> list) {
        int i = messageMapper.updateDeleteById(list);
        return i;
    }

    @Override
    public int updateRecoverById(List<Message> list) {
        for (int i=0; i<list.size(); i++){
            if(list.get(i).getMessageStatus() != 3){
                list.remove(i);
            }else{
                list.get(i).setMessageStatus(2);
            }
        }
        int i = messageMapper.updateMessageStatusById(list);
        return i;
    }

    @Override
    public Message selectDraftByUserId(Long userId) {
        return messageMapper.selectDraftByUserId(userId);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(Message record) {
        return messageMapper.insert(record);
    }

    @Override
    public int insertSelective(Message record) {
        record.setDr(0);
        record.setEnableStatus(2);
        record.setMessageStatus(1);
        record.setCreateTime(new Date());
        record.setSendTime(new Date());
        return messageMapper.insertSelective(record);
    }

    @Override
    public Message selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(Message record) {
        record.setDr(0);
        record.setEnableStatus(2);
        record.setMessageStatus(1);
        return messageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(Message record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Message record) {
        return 0;
    }
}